package com.agu.config;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.CacheResolver;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.cache.interceptor.SimpleKeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.agu.utils.CacheUtil;

import net.sf.ehcache.config.CacheConfiguration;

/**
 * 缓存配置 
 * 使用EhCache实现
 * @author Sansan
 *
 */
@Configuration
@EnableCaching
public class CacheConfig implements CachingConfigurer {

	@Bean(destroyMethod="shutdown")
    public net.sf.ehcache.CacheManager ehCacheManager() {
		CacheConfiguration monitorCacheConfiguration = new CacheConfiguration();
        monitorCacheConfiguration.setName(CacheUtil.CACHE_MONITOR);
        monitorCacheConfiguration.setMemoryStoreEvictionPolicy("LRU");
        monitorCacheConfiguration.setMaxEntriesLocalHeap(1000);
        
        net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();
        config.addCache(monitorCacheConfiguration);
        
        return net.sf.ehcache.CacheManager.newInstance(config);
    }
	
	@Bean
	@Override
	public CacheManager cacheManager() {
	    return new EhCacheCacheManager(ehCacheManager());
	}
	
	@Bean
	@Override
	public KeyGenerator keyGenerator() {
		return new SimpleKeyGenerator();
	}
	
	@Override
	public CacheResolver cacheResolver() {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public CacheErrorHandler errorHandler() {
		// TODO Auto-generated method stub
		return null;
	}
}
